

package com.mt.mall.controller.good;

import org.springframework.security.access.prepost.PreAuthorize;
import com.mt.common.core.annotation.ApiPageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.mt.common.core.web.base.PageDTO;
import com.mt.common.core.web.base.PageResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.mt.mall.dto.good.CommodityEditDto;
import com.mt.mall.entity.good.Commodity;
import com.mt.mall.service.good.CommodityService;
import com.mt.common.system.service.UserService;
import com.mt.mall.service.merchant.ShopService;


@Api(tags = "商品")
@RestController
@RequestMapping("/api/mall/good/Commodity")
@CrossOrigin(allowCredentials = "true")
public class CommodityController {
    private static Logger logger = LoggerFactory.getLogger(CommodityController.class);


    @Autowired
    private CommodityService commodityService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShopService shopService;

    /**
     * 根据分页参数查询商品集合
     *
     * @param pageDTO 分页条件
     */
    //@PreAuthorize("hasAuthority('mall:good:Commodity:view')")
    @ApiOperation("分页查询商品")
    @ApiPageParam
    @PostMapping("/findCommoditys")
    public PageResultDTO findCommoditys(@RequestBody PageDTO pageDTO) {
        return this.commodityService.findCommoditys(pageDTO);
    }

    /**
     * 根据ID查询指定的商品
     *
     * @param commodityId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Commodity:view')")
    @ApiOperation("根据id查询商品")
    @ApiPageParam
    @PostMapping("/findCommodity")
    public Commodity findCommodity(@RequestParam Long commodityId) {
        return this.commodityService.findCommodity(commodityId);
    }

    /**
     * 根据ID查询指定的商品(包含外键名称)
     *
     * @param commodityId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Commodity:view')")
    @ApiOperation("根据ID查询指定的商品(包含外键名称)")
    @PostMapping("/findCommodityForView")
    public Commodity findCommodityForView(@RequestParam Long commodityId) {
        return this.commodityService.findCommodityWithForeignName(commodityId);
    }

    /**
     * 根据ID查询指定的商品(包含商品和外键名称)
     *
     * @param commodityId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Commodity:view')")
    @ApiOperation("根据ID查询指定的商品(包含商品和外键名称)")
    @PostMapping("/findCommodityForEdit")
    public CommodityEditDto findCommodityForEdit(@RequestParam Long commodityId) {
        CommodityEditDto commodityEditDto = new CommodityEditDto();
        commodityEditDto.setCommodity(this.commodityService.findCommodityWithForeignName(commodityId));

        this.prepareCommodityEditDto(commodityEditDto);

        return commodityEditDto;
    }

    /**
     * 根据ID查询指定的商品(只提取ID 和 Name)
     *
     * @param commodityId Id
     */
    @PreAuthorize("hasAuthority('mall:good:Commodity:view')")
    @ApiOperation("根据ID查询指定的商品(只提取ID 和 Name)")
    @PostMapping("/findCommoditysWithIdNameById")
    public Commodity findCommoditysWithIdNameById(@RequestParam Long commodityId) {
        return this.commodityService.findCommoditysWithIdNameById(commodityId);
    }

    /**
     * 根据名称查询商品集合(只提取ID 和 Name)
     *
     * @param commodityName 名称
     */
    @PreAuthorize("hasAuthority('mall:good:Commodity:view')")
    @ApiOperation("根据名称查询商品集合(只提取ID 和 Name)")
    @PostMapping("/findCommoditysWithIdNameByName")
    public List<Commodity> findCommoditysWithIdNameByName(@RequestParam String commodityName) {
        return this.commodityService.findCommoditysWithIdNameByName(commodityName);
    }


    /**
     * 创建新的商品
     */
    @PreAuthorize("hasAuthority('mall:good:Commodity:add')")
    @ApiOperation("创建新的商品")
    @PostMapping("/createCommodity")
    public CommodityEditDto createCommodity() {
        CommodityEditDto commodityEditDto = new CommodityEditDto();
        commodityEditDto.setCommodity(new Commodity());

        this.prepareCommodityEditDto(commodityEditDto);
        return commodityEditDto;
    }

    private void prepareCommodityEditDto(CommodityEditDto commodityEditDto) {
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        commodityEditDto.setUserUsers(this.userService.findAllUsersWithIdName());
        //TODO: 以下代码可以注释掉，此行代码即时加载所有外键对象，以便选择。如果不在此加载，可以在界面上实现延迟加载。如果外键对象超过 500 行，建议采用延迟加载
        commodityEditDto.setShopShops(this.shopService.findAllShopsWithIdName());
    }

    /**
     * 新增保存商品
     *
     * @param commodity 实体对象
     */
    @PreAuthorize("hasAuthority('mall:good:Commodity:add')")
    @ApiOperation("新增保存商品")
    @PostMapping("/saveCommodity")
    public Commodity saveCommodity(@RequestBody Commodity commodity) {
        return this.commodityService.saveCommodity(commodity);
    }

    /**
     * 修改保存商品
     *
     * @param commodity 实体对象
     */
    @PreAuthorize("hasAuthority('mall:good:Commodity:update')")
    @ApiOperation("修改保存商品")
    @PostMapping("/updateCommodity")
    public Commodity updateCommodity(@RequestBody Commodity commodity) {
        return this.commodityService.updateCommodity(commodity);
    }

    /**
     * 根据ID删除商品
     *
     * @param commodityId ID
     */
    @PreAuthorize("hasAuthority('mall:good:Commodity:remove')")
    @ApiOperation("根据ID删除商品")
    @PostMapping("/deleteCommodity")
    public void deleteCommodity(@RequestParam Long commodityId) {
        this.commodityService.deleteCommodity(commodityId);
    }

    /*
    同步数据到ES
     */
    @ApiOperation("同步数据到ES")
    @PostMapping("/synchronousAllData")
    public boolean synchronousAllData() {
       return this.commodityService.synchronousAllData();
    }

    /*
    同步数据到ES
     */
    @ApiOperation("es中查询数据")
    @PostMapping("/findCommoditysInES")
    public PageResultDTO findCommoditysInES(@RequestBody PageDTO pageDTO) {
        return this.commodityService.findCommoditysInES(pageDTO);
    }
    /*
    查询销量爆款
     */
    @ApiOperation("")
    @PostMapping("/findHotCommodity")
    public List<Commodity> findHotCommodity() {
        return this.commodityService.findHotCommodity();
    }
}

